package com.srs.backend.serviceImpl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.srs.backend.mapper.ManagerMapper;
import com.srs.backend.pojo.Manager;
import com.srs.backend.service.ManagerService;
import com.srs.backend.utils.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;
import java.util.List;
@Service
public class ManagerServiceImpl implements ManagerService {
    @Resource
    private ManagerMapper managerMapper;
    @Override
    public Result add(Map<String, String> data) {
        String managername = data.get("name");
        String sex = data.get("sex");
        String phone = data.get("phone");
        String remark = data.get("remark");
        Integer uid;

        try {
            uid = Integer.parseInt(data.get("uid"));
        } catch (Exception e) {
            return Result.error("uid有误");
        }

        if (managername == null || managername.length() == 0) {
            return Result.error("管理员名称不能为空");
        }

        managerMapper.insert(new Manager(null,managername,sex,phone,remark, uid));

        return Result.ok("success");

    }
    @Override
    public Result delete(Map<String, String> data){
        String idStr = data.get("id");
        Integer id = null;
        try {
            id = Integer.parseInt(idStr);
        } catch (NumberFormatException e) {
            return Result.error("id数据有误");
        }
        Manager curManager = managerMapper.selectById(id);
        if (curManager == null) {
            return Result.error("当前操作管理员不存在或已被删除");
        }
        managerMapper.deleteById(curManager);

        return Result.ok("success");
    }
    @Override
    public Result modify(Map<String, String> data){
        String idStr = data.get("id");
        Integer id = null;
        try {
            id = Integer.parseInt(idStr);
        } catch (NumberFormatException e) {
            return Result.error("id数据有误");
        }
        Manager curManager = managerMapper.selectById(id);
        if (curManager == null) {
            return Result.error("当前操作管理员不存在或已被删除");
        }

        String managername = data.get("name");
        String sex = data.get("sex");
        String phone = data.get("phone");
        String remark = data.get("remark");

        if (managername == null || managername.length() == 0) {
            return Result.error("管理员名称不能为空");
        }

        curManager.setName(managername);
        curManager.setSex(sex);
        curManager.setPhone(phone);
        curManager.setRemark(remark);

        managerMapper.updateById(curManager);
        return Result.ok("success");
    }

    @Override
    public Result getAll(){
        List<Manager> managers = managerMapper.selectList(null);
        for (int i = 0; i < managers.size(); i ++ ) {
            if (managers.get(i).getId() != null) {
                Manager curManager = managerMapper.selectById(managers.get(i).getId());
                managers.get(i).setName(curManager.getName());
            }
        }
        return Result.ok(managers);
    }
    @Override
    public Result getManagerInfo(Map<String, String> data) {
        String uidStr = data.get("uid");
        Integer uid;
        try {
            uid = Integer.parseInt(uidStr);
        } catch (Exception e) {
            return  Result.error("uid类型有误");
        }
        QueryWrapper<Manager> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", uid);
        Manager manager = managerMapper.selectOne(queryWrapper);
        if (manager == null) {
            return Result.error("该 管理员 不存在或已被删除");
        }
        return Result.ok(manager);
    }
}
